/**
 * 
 * @Author: biubiu0002
 * @Date: 2021-04-26 11:43
 **/


#include <CourseMapper.h>
#include "CourseService.h"
#include "iostream"
#include "fstream"
#include "sstream"
#include "iomanip"
#include "list"
#include "vector"
#include "string"

using namespace std;

int CourseService::matchCourseByInput(){
    cout<<"请输入搜索关键词(多个关键词用空格或换行分隔 输入0结束 ):"<<endl;
    vector<string> keys;
    string key;
    while(true){
        cin>>key;
        if(key=="0")
            break;
        keys.push_back(key);
    }
    CourseMapper courseMapper;
    list<Course> courseList = courseMapper.getCourseList();
    vector<Course> resultList;

    for(Course course:courseList){
        int findFlag=1;
        string courseName = course.getName();
        for(string curKey:keys){
            if(courseName.find(curKey)==courseName.npos){
                findFlag=0;
                break;
            }
        }
        if(findFlag){
            resultList.push_back(course);
        }
    }
    showCourseList(resultList);





    return 0;


}

int CourseService::showAllCourseList() {
    CourseMapper courseMapper;
    list<Course> courseList = courseMapper.getCourseList();
    return this->showCourseList(courseList);
}

int CourseService::batchInsertCourse() {
    CourseMapper courseMapper;
    list<Course> courseList = courseMapper.getCourseList();
    int onceCommitChoice = -1;
    list<Course> tempCourseList;
    while(true){
        string field;
        cout<<"请输入课程编号(6位统一编号):";
        cin>>field;
        int idIsRepeated = this->checkId(field,tempCourseList);
        if(idIsRepeated){
            cout<<"课程编号有重复！请重试"<<endl;
            if(isContinue(courseList,tempCourseList)){
                continue;
            }
            return 0;

        }
        Course* course = new Course();
        course->setId(field);
        cout<<"请输入课程名称：";cin>>field;
        course->setName(field);
        cout<<"请输入课程学分：";cin>>field;
        course->setCredit(stof(field));

        cout<<endl<<"您输入的课程信息是："<<endl;
        cout<<"课程编号\t名称\t学分"<<endl;
        cout<<course->getId()<<" "<<course->getName()<<" ";
        cout<<fixed<<setprecision(1)<<course->getCredit()<<endl;
        cout<<endl<<"确认该条记录正确吗?(1.正确，加入到暂存区  2.错误，重新输入  0.放弃输入，退回上一页面)";
        cin>>onceCommitChoice;
        if(onceCommitChoice==0){
            cout<<"退出成功";
            system("pause");
            system("cls");
            return 0;
        }
        else if(onceCommitChoice==2){
            continue;
        }
        tempCourseList.push_back(*course);
        cout<<"当前已暂存的信息："<<endl;
        cout<<"课程编号\t名称\t学分"<<endl;
        for(Course tempCourse:tempCourseList){
            cout<<tempCourse.getId()<<" "<<tempCourse.getName()<<" ";
            cout<<fixed<<setprecision(1)<<course->getCredit()<<endl;
        }
        if(isContinue(courseList,tempCourseList)){
            continue;
        }
        return 0;


    }

}

int CourseService::checkId(std::string id, list<Course> &tempCourseList) {
    CourseMapper courseMapper;
    list<Course> courseList = courseMapper.getCourseList();

    for(Course course:courseList){
        if(course.getId()==id){
            return 1;
        }
    }
    for(Course course:tempCourseList){
        if(course.getId()==id){
            return 1;
        }
    }

    return 0;
}



int CourseService::isContinue(list<Course> &courseList, list<Course> &tempCourseList) {
    int continueChoice,saveResultChoice;
    cout<<endl<<"是否继续录入？(1.继续录入 0.结束录入)";
    cin>>continueChoice;
    if(continueChoice==1){
        return 1;
    }
    else if(continueChoice==0){
        cout<<"是否保存结果信息？(1.保存 0.放弃，本次录入将不被保存)"<<endl;
        cin>>saveResultChoice;
        if(saveResultChoice){
            for(Course tempCourse:tempCourseList){
                courseList.push_back(tempCourse);
            }
            CourseMapper courseMapper;
            courseMapper.setCourseList(courseList);
            courseMapper.batchInsertCourse(courseList);
            cout<<"保存成功！"<<endl;
        }
        else{
            cout<<"退出成功。"<<endl;
        }
        system("pause");
        system("cls");
        return 0;
    }

    cout<<"程序出错啦，回到上一页面"<<endl;
    system("pause");
    system("cls");
    return 0;
}

int CourseService::showCourseList(vector<Course> &courseList) {
    for(Course course:courseList){

        cout<<course.getId()<<" ";
        cout<<course.getName()<<" ";
        cout<<fixed<<setprecision(1)<<course.getCredit()<<endl;

    }
    cout<<"共"<<courseList.size()<<"条记录."<<endl;
    return 0;
}

int CourseService::showCourseList(list<Course> &courseList) {
    for(Course course:courseList){

        cout<<course.getId()<<" ";
        cout<<course.getName()<<" ";
        cout<<fixed<<setprecision(1)<<course.getCredit()<<endl;

    }
    cout<<"共"<<courseList.size()<<"条记录."<<endl;
    return 0;
}

CourseService::CourseService() {

}

CourseService::~CourseService() {}

